﻿//**************************************************
function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == "0") {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}
//**************************************************
function setupHtmlCombo(elementName, html){
    document.getElementById(elementName).name = "__"
            
    document.getElementById(elementName).parentNode.innerHTML = html

}
//**************************************************
function detectBrowser(){
    var mReturnValue = null
    var mAgent = new String(navigator.userAgent.toLowerCase())

    if (mAgent.indexOf("msie") !== -1){
        mReturnValue = "IE"
    }else if(mAgent.indexOf("firefox") !== -1){
        mReturnValue = "FF"
    }else if(mAgent.indexOf("chrome") !== -1){
        mReturnValue = "CHR"
    }else if(mAgent.indexOf("safari") !== -1){
        mReturnValue = "SF"
    }else if(mAgent.indexOf("opera") !== -1){
        mReturnValue = "OP"
    }
        
    return mReturnValue

}

//**************************************************
function stopEvent(event){
    if (event == null){
        return
    }
    
    var mBrowser = detectBrowser()
    
    switch (mBrowser){
        case "IE":
            event.returnValue = false
            break;
        case "FF":
            event.preventDefault()
            break;
        case "SF":
            event.preventDefault()
            event.stopPropagation()
            break;
        case "OP":
            event.preventDefault()
            break;            
    }
}
//**************************************************
function OpenPopup(url, title, width, height, a, fid, f){
  window.setTimeout(function(){
    jQuery.facebox('<div style="width:'+width+'px; height:'+height+'px"><iframe src="'+url+'" frameborder="0" width="100%" height="100%" id="facebox-iframe" class="frame-container"></iframe></div>',false,"simpleFacebox",false)
  }, 100)
  

  if (fid!=null && f !=null){
    arrRefFunctions[fid] = f;
  }
}
function OpenMultiPopup(url, title, width, height, a, fid, f,key,titleclass,isedit,showSaveButton){
if ((showSaveButton==undefined)||(showSaveButton==null)){
  showSaveButton = true
}
  window.setTimeout(function(){
    jQuery.facebox('<div style="width:'+width+'px; height:'+height+'px"><iframe src="'+url+'" frameborder="0" width="100%" height="100%" id="facebox-iframe-'+key+'" class="frame-container"></iframe></div>',
    false,
    key,
    true,title,titleclass,isedit,showSaveButton)
  }, 100)

  if (fid!=null && f !=null){
    arrRefFunctions[fid] = f;
  }
}
//**************************************************
function ClosePopup(){
  var faceboxWindow = document.getElementById("facebox-iframe").contentWindow
  if (faceboxWindow.WindowClose)
    faceboxWindow.WindowClose()

    $.facebox.close("simpleFacebox")
//  $(document).trigger('close.facebox');  
}
//**************************************************
function CloseMultiPopup(key){
  var faceboxWindow = document.getElementById("facebox-iframe-"+key);
  if(faceboxWindow){
    if(faceboxWindow.contentWindow){
      if (faceboxWindow.WindowClose){
        faceboxWindow.WindowClose();
      }
    } 
  }   
  $.facebox.close(key)
}
//**************************************************
function notSafariPostBack(){
  try{
  if(detectBrowser()=="SF"){
   var f=document.forms[0];
   if(f){
    f.onsubmit=function(){return false;}
   }  
  }
  }catch(e){} 
}
//**************************************************
function notPostBack(){
 try{
  var f=document.forms[0];
  if(f){
   f.onsubmit=function(){return false;}
  }   
 }catch(e){}
}
//**************************************************
function getElementsByNameEx(name){
  var output = null
  var collection = document.getElementsByName(name)
  
  if (collection.length !== 0){
    output = collection
  }else{
    output = new Array()
    output.push(collection)
  
  }
  
  return output 

}
//**************************************************
function remove_options(obj){
	var j = obj.length
	for (i=0; i<j; i++){
		obj.remove(0)
	}
}
//**************************************************************************
function addOption(objNm, valueID, valueText) {
	var idx = window.document.getElementById(objNm).options.length;
  var newOpt = new Option(valueText, valueID);
  
  window.document.getElementById(objNm).options[idx] = newOpt
}
//**************************************************
function ApplyColorPickerMini(ctrlShowPicker, ctrlShowValue){

  $("#" + ctrlShowPicker).ColorPicker({
	  onSubmit: function(hsb, hex, rgb){
		  $("#" + ctrlShowValue).val("#" + hex);		  
		  $("#" + ctrlShowValue).change()
	  },
	  mini: true,
	  onBeforeShow: function () {
  		  $(this).ColorPickerSetColor($("#" + ctrlShowValue).val().replace("#", ""));
	  }
  })
  .bind('keyup', function(){
	  $(this).ColorPickerSetColor($("#" + ctrlShowValue).val().replace("#", ""));
  });

}
//**************************************************
function ApplyColorPicker(ctrlShowPicker, ctrlShowValue){

  $("#" + ctrlShowPicker).ColorPicker({
	  onSubmit: function(hsb, hex, rgb){
		  $("#" + ctrlShowValue).val("#" + hex);		  
		  $("#" + ctrlShowValue).change()
	  },
	  onBeforeShow: function () {
  		  $(this).ColorPickerSetColor($("#" + ctrlShowValue).val().replace("#", ""));
	  }
  })
  .bind('keyup', function(){
	  $(this).ColorPickerSetColor($("#" + ctrlShowValue).val().replace("#", ""));
  });

}
//**************************************************
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
//**************************************************************************
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//*****************************************************************************
function getCheckboxValue(elementName){
  var elementValue = 0
  
  var collection = document.getElementsByName("rdLandingPages")
  
  for (i=0;i<collection.length;i++){
    if (collection[i].checked){
      return collection[i].value      
    }    
  }
}
